// 数据验证函数库
// by indraw 2013.01.01
package util

import (
    "fmt"
    "strings"
    "regexp"
    "net/http"
    "time"
    //"strconv"
)

/**
 * 判断是否为空
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckValue(str string) bool {
    if len(str)==0{
        return false
    }
    return true
}

/**
 * 判断是否为空
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckLen(str string, l int) bool {
    if len(str)<l{
        return false
    }
    return true
}

/**
 * 判断是否为空
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckLenMax(str string, l int) bool {
    if len(str)>l{
        return false
    }
    return true
}

/**
 * 判断是否为数字
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckNumber(str string) bool {
    if m, _ := regexp.MatchString("^[0-9]+$", str); !m {
        return false
    }
    return true
}



/**
 * 判断是否为中文
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckCN(str string) bool {
    if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{9fa5}]+$", str); !m {
        return false
    }
    return true
}



/**
 * 判断是否为英文
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckEN(str string) bool {
    if m, _ := regexp.MatchString("^[a-zA-Z]+$", str); !m {
        return false
    }
    return true
}



/**
 * 判断是否为邮件地址
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckEmail(str string) bool {
    if m, _ := regexp.MatchString(`^([\w\.\_]{2,20})@(\w{1,}).([a-z]{2,4})$`, str); !m {
        return false
    }
    return true
}

/**
 * 判断是否为手机
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckMobile(str string) bool {
    if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, str); !m {
        return false
    }
    return true
}



/**
 * 判断是否为身份证
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckIdcard(str string) bool {
    if m, _ := regexp.MatchString(`^(\d{17})([0-9]|X)$`, str); !m {
        return false
    }
    return true
}



/**
 * 判断单选
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckInarray(arr []string, str string) bool {
    for _, v := range arr {
        if v == str{
            return true
        }
    }
    return false
}

/**
 * 判断是否为0或1
 *
 * author: indraw
 * date: 2013-01-01
 */
func CheckEnum( str string) bool {
    if str=="1" || str=="0"{
		return true
	}
    return false
}

/**
 * 获取客户IP
 *
 * author: indraw
 * date: 2013-01-01
 */
func GetUserIP(re *http.Request) string {
    
    fmt.Println(re.RemoteAddr)
    ips := strings.Split(re.RemoteAddr,":")
    return ips[0]
}


/**
 * 获取客户IP
 *
 * author: indraw
 * date: 2013-01-01
 */
func GetUnixTime() int {
    
    return int(time.Now().Unix())
}

/**
 * 获取客户输入合法信息
 *
 * author: indraw
 * date: 2013-01-01
 */
func GetInputContent(input string) string {
    
    return input
}


























